# Based on AUR's lem-editor-git & qlot
# Contributor: Celeste <cielesti@protonmail.com>
# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=lem
pkgver=2.2.0
pkgrel=0
pkgdesc="Common Lisp editor/IDE with high expansibility"
url="https://lem-project.github.io/"
arch="aarch64 x86_64"
license="MIT"
makedepends="
	libdecor-dev
	ncurses-dev
	sbcl
	sdl2-dev
	sdl2_image-dev
	sdl2_ttf-dev
	"
checkdepends="roswell"
subpackages="
	$pkgname-common
	$pkgname-gui
	"
_qlotver=1.5.1
source="https://github.com/lem-project/lem/archive/refs/tags/v$pkgver/lem-$pkgver.tar.gz
	https://github.com/fukamachi/qlot/releases/download/$_qlotver/qlot-$_qlotver.tar.gz
	aarch64-disable-timer-tests_patch
	build-ncurses.lisp
	build-sdl2.lisp
	general.lisp
	lem.desktop
	"
options="!strip"

prepare() {
	default_prepare

	cd "$srcdir"/qlot
	sbcl --noinform --no-sysinit --no-userinit --non-interactive \
		--load .bundle-libs/bundle.lisp \
		--eval "(asdf:load-asd #P\"$srcdir/qlot/qlot.asd\")" \
		--eval '(let ((*standard-output* (make-broadcast-stream))
				(*trace-output* (make-broadcast-stream)))
			(mapc (function asdf:load-system)
				(list :qlot :qlot/subcommands :qlot/cli :qlot/fetch)))'

	cd "$builddir"
	"$srcdir"/qlot/scripts/run.sh install

	case "$CARCH" in
	aarch64)
		patch lem-tests.asd \
		"$srcdir"/aarch64-disable-timer-tests_patch
		;;
	esac
}

build() {
	msg "Building lem"
	sbcl --noinform --no-sysinit --no-userinit \
		--load .qlot/setup.lisp \
		--load "$srcdir"/build-ncurses.lisp

	msg "Building lem-gui"
	sbcl --noinform --no-sysinit --no-userinit \
		--load .qlot/setup.lisp \
		--load "$srcdir"/build-sdl2.lisp
}

check() {
	.qlot/bin/rove lem-tests.asd
}

package() {
	depends="
		$pkgname-common=$pkgver-r$pkgrel
		libcrypto3
		libssl3
		so:libncursesw.so.6
		"

	install -dvm755 "$pkgdir"/usr/share/lem
	cp -vr frontends/sdl2/resources "$pkgdir"/usr/share/lem/

	# Devendor Noto fonts
	rm -v "$pkgdir"/usr/share/lem/resources/fonts/NotoColorEmoji.ttf \
		"$pkgdir"/usr/share/lem/resources/fonts/NotoSansCJK-Bold.ttc \
		"$pkgdir"/usr/share/lem/resources/fonts/NotoSansCJK-Regular.ttc
	ln -sv /usr/share/fonts/noto/NotoColorEmoji.ttf \
		"$pkgdir"/usr/share/lem/resources/fonts/
	ln -sv /usr/share/fonts/noto/NotoSansCJK-Bold.ttc \
		"$pkgdir"/usr/share/lem/resources/fonts/
	ln -sv /usr/share/fonts/noto/NotoSansCJK-Regular.ttc \
		"$pkgdir"/usr/share/lem/resources/fonts/

	install -Dvm755 lem lem-gui -t "$pkgdir"/usr/bin/
	install -Dvm755 "$srcdir"/libasyncprocess.so \
		-t "$pkgdir"/usr/share/lem/
	install -Dvm644 "$srcdir"/lem.desktop \
		-t "$pkgdir"/usr/share/applications/
	install -Dvm644 LICENCE \
		"$pkgdir"/usr/share/licenses/$pkgname/LICENSE
}

common() {
	pkgdesc="$pkgdesc (common files)"
	depends=

	amove usr/share/lem/libasyncprocess.so
	amove usr/share/licenses/$pkgname
}

gui() {
	pkgdesc="$pkgdesc (graphical variant)"
	depends="
		$pkgname-common=$pkgver-r$pkgrel
		font-noto-cjk
		font-noto-emoji
		libcrypto3
		libssl3
		so:libdecor-0.so.0
		so:libffi.so.8
		so:libsamplerate.so.0
		so:libtiff.so.6
		so:libSDL2-2.0.so.0
		so:libSDL2_image-2.0.so.0
		so:libSDL2_ttf-2.0.so.0
		"

	amove usr/bin/lem-gui
	amove usr/share/applications/lem.desktop
	amove usr/share/lem/resources
}

sha512sums="
db4ea4bf1f4cf5dbc6f29c8e528bf05214e58d520dd11c9eab032e2e478b9612f2378827796e56133e6d58bf2297c8f1a8f693ae77bb4d85bb63369480573301  lem-2.2.0.tar.gz
6bc3b1f0f3e4e745af08f47249aa8693539ae441478db46c95140caa757561e161f06492a73b0fff366fffab3c2e5303c74dc2d3a8686a2450106bd27ce50c6e  qlot-1.5.1.tar.gz
7f7f3a2d5d6fd4fab567e9c07284e6b9919857802ecdeffa4dd1e42078ba7b51133c8db7c8efcf2c33448c2d741f734c96b3ed8f68059489b2c32de1ccc2060c  aarch64-disable-timer-tests_patch
44ad20459851217cf5c5cc9d1a75583b5ebf1de1de6cece44f0073042420c4d6533838ae5f785d40c25cfd7d71b589d39be076f3ad7c6ef2e53e9a574e134d91  build-ncurses.lisp
b4f4786cd9ae1e122d142f9957b758b0c2a5bb4ef444570a682298c22b58c92f8966684c1aeb306ee13e834539daadfa13a84663adab3fb1604681b8a66d82bf  build-sdl2.lisp
07094800689a2cba48c21de6c9086961eebab8a47f21f153041d76f3d6a85ea98653fb21ef6a81a2e4aa07a25f1f36736e50ca858614135ba07586062b0d5364  general.lisp
e78665b79229caf77b432fff89241de7fab1746ba6e5a27de9fafbe8ffebc8fae858e0653d29ff47db205b7e07b64526efb21fae416ef83913ac0fd804a78916  lem.desktop
"
